<template>
	<view class="content">
		<view class="logo">
			<image src="../../static/login/logo.png" mode=""></image>
		</view>
		<view class="uni-form-item uni-column">
			<input v-model="phone" type="tel" class="uni-input" name="" placeholder="请输入手机号" />
		</view>
		<view class="uni-form-item uni-column column-with-btn">
			<input v-model="smscode" type="number" class="uni-input" name="" placeholder="请输入验证码" />
			<button :class="{active : !disableCodeBtn}" :disabled="disableCodeBtn"
				@tap="sendCode">{{codeBtn.text}}</button>
		</view>
		<view class="uni-form-item uni-column">
			<input v-model="password" type="password" class="uni-input" name="" placeholder="请输入密码" />
		</view>
		<view class="uni-form-item uni-column">
			<input v-model="passwordConfirm" type="password" class="uni-input" name="" placeholder="请再次输入密码" />
		</view>
		<view class="uni-form-item uni-column">
			<input maxlength="6" v-model="paymentPassword" type="password" class="uni-input" name="" placeholder="请输入6位支付密码" />
		</view>
		<view class="uni-form-item uni-column">
			<input maxlength="6" v-model="paymentPasswordConfirm" type="password" class="uni-input" name="" placeholder="请再次输入支付密码" />
		</view>
		<view class="uni-form-item uni-column">
			<input v-model="nickName" class="uni-input" name="" placeholder="请输入昵称" />
		</view>
		<view class="uni-form-item uni-column">
			<input v-model="recommendUserUid" class="uni-input" name="" placeholder="邀请码" />
		</view>
		<button type="primary" @tap="registerUser()">注册</button>
		<view class="links">已有账号？<view class="link-highlight" @tap="gotoLogin">点此登陆</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				nickName: "",
				password: "",
				passwordConfirm: "",
				paymentPassword: "",
				paymentPasswordConfirm: "",
				phone: "",
				recommendUserUid: "778683",
				smscode: "",
				seconds: 60,
				codeBtn: {
					text: '获取验证码',
					waitingCode: false,
					count: this.seconds
				}
			}
		},
		onLoad() {

		},
		methods: {
			sendCode: function() {
				var me = this;
				if (me.phone.length == 0) {
					me.$api.msg('请输入手机号');
					return;
				}
				if (me.phone.length != 11) {
					me.$api.msg('请输入合法手机号');
					return;
				}
				uni.showLoading({
					title:''
				})
				uni.request({
					url: me.serverUrl + '/core/api/v1/common/sendSms/register',
					method: "POST",
					data: {
						'phoneNumber': me.phone
					},
					header: {
						'content-type': 'application/json'
					},
					success: (res) => {
						me.codeBtn.waitingCode = true;
						me.codeBtn.count = me.seconds;
						me.codeBtn.text = me.codeBtn.count + 's';

						let countdown = setInterval(() => {
							me.codeBtn.count--;
							me.codeBtn.text = me.codeBtn.count + 's';
							if (me.codeBtn.count < 0) {
								clearInterval(countdown);
								me.codeBtn.text = '重新发送';
								me.codeBtn.waitingCode = false;
							}
						}, 1000);
					},
					complete: (error) => {
						uni.hideLoading();
						console.log(error)
					}
				})
			},
			registerUser: function() {
				var me = this;
				if (me.phone.length == 0) {
					me.$api.msg('请输入手机号');
					return;
				}
				if (me.phone.length != 11) {
					me.$api.msg('请输入合法手机号');
					return;
				}
				if (me.smscode.length == 0) {
					me.$api.msg('请输入验证码');
					return;
				}
				if (me.password.length == 0) {
					me.$api.msg('请输入密码');
					return;
				}
				if (me.password != me.passwordConfirm) {
					me.$api.msg('请保证登陆密码一致');
					return;
				}
				if (me.paymentPassword.length == 0) {
					me.$api.msg('请输入支付密码');
					return;
				}
				if (me.paymentPasswordConfirm.length == 0) {
					me.$api.msg('请输入确认支付密码');
					return;
				}
				if (me.paymentPassword.length < 6) {
					me.$api.msg('请输入6位支付密码');
					return;
				}
				if (me.paymentPasswordConfirm.length < 6) {
					me.$api.msg('请输入6位确认支付密码');
					return;
				}
				if (me.paymentPassword != me.paymentPasswordConfirm) {
					me.$api.msg('请保证支付密码一致');
					return;
				}
				uni.showLoading({
					title:''
				})
				uni.request({
					url: me.serverUrl + '/core/api/v1/hy/passport/register',
					method: "POST",
					data: {
						'nickName': me.nickName,
						'password': me.password,
						'passwordConfirm': me.passwordConfirm,
						'paymentPassword': me.paymentPassword,
						'paymentPasswordConfirm': me.paymentPasswordConfirm,
						'phone': me.phone,
						'recommendUserUid': me.recommendUserUid,
						'smscode': me.smscode,
						'sign': me.randomWord(false, 32)
					},
					header: {
						'content-type': 'application/json'
					},
					success: (res) => {
						if (res.data.code == 200) {
							me.$api.msg('注册成功');
							uni.navigateBack({
								
							})
						} else {
							
							uni.hideLoading();
							me.$api.msg(res.data.msg);
						}
					},
					complete: (error) => {
						// uni.hideLoading()
						console.log(error)
					}
				})
			},
			gotoLogin: function() {
				uni.navigateBack({

				})
			},
			randomWord: function(randomFlag,min,max) {
				var str = "";
				var range = min;
				var arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];

				//随机产生
				if (randomFlag) {
					range = Math.round(Math.random() * (max - min)) + min;
				}

				for (var i = 0;i < range; i++) {
					var pos = Math.round(Math.random() * (arr.length - 1));

					str += arr[pos];

					return str;
				}
			},
		},
		computed: {
			disableCodeBtn: function() {
				return this.codeBtn.waitingCode;
			}
		}
			
	}
</script>

<style lang="scss" scoped>
	$color-primary: #12B3F9;

	.content {
		padding: 60upx 100upx 100upx;
	}

	.logo {
		text-align: center;

		image {
			height: 200upx;
			width: 200upx;
			margin: 0 0 40upx;
		}
	}

	.uni-form-item {
		margin-bottom: 40upx;
		padding: 0;
		border-bottom: 1px solid #e3e3e3;

		.uni-input {
			font-size: 30upx;
			padding: 7px 0;
		}
	}

	.column-with-btn {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;

		button {
			font-size: 24upx;
			margin: 0;
			width: 180upx;
			text-align: center;

			&:after {
				border: none
			}

			&.active {
				background-color: $color-primary;
				color: $uni-text-color-inverse;
			}
		}
	}

	.img-captcha {
		width: 150upx;
		height: 60upx;
	}

	button[type="primary"] {
		background-color: $color-primary;
		border-radius: 0;
		font-size: 34upx;
		margin-top: 60upx;
	}

	.links {
		text-align: center;
		margin-top: 40upx;
		font-size: 26upx;
		color: #999;

		view {
			display: inline-block;
			vertical-align: top;
			margin: 0 10upx;
		}

		.link-highlight {
			color: $color-primary
		}
	}
</style>
